package com.customization.unionpay.market.util;

import com.alibaba.fastjson.JSONObject;
import com.engine.cube.biz.ShardingActionHelper;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

/**
 * @author sjm
 * @description 日报表内置表相关
 */
public class DailyReportBuiltMapping {

    private static final Logger log = LoggerFactory.getLogger(DailyReportBuiltMapping.class);

    /**
     * 添加日报表内置表数据
     *
     * @param formId
     * @param formmodeId
     * @param user
     * @param maintablename
     * @param qkpch
     * @param zjly
     * @param actTp
     * @param fl
     * @param fkzy
     * @param hzje
     * @param hblx
     * @param jsfs
     * @param fkzh
     * @param skzh
     * @param sqmwh
     */
    public static void addDailyReportData(int formId, int formmodeId, User user, String maintablename,
                                          String qkpch, String zjly, String actTp,
                                          String fl, String fkzy, String hzje, String hblx,
                                          String jsfs, String fkzh,String fkzhkhh,String fkzhzh,String fkzhyhzh,String skzh,String skzhkhh,String skzhzh,String skzhyhzh, String sqmwh) {

        //        请款批次号  qkpch
        //        资金来源    zjly
        //        活动类型    actTp
        //        分类       fl
        //        付款摘要    fkzy
        //        汇总金额    hzje
        //        划拨类型    hblx
        //        结算方式    jsfs
        //        付款账户    fkzh
        //        收款账户    skzh

        RecordSetTrans rst = new RecordSetTrans();
        // 开启事务（关闭自动提交）
        rst.setAutoCommit(false);

        // 先调用内部方法新建一条数据，获取数据id
        int billid = FormDataUtils.getBillId(maintablename, formmodeId, user.getUID(), user.getType());

        try {
            // 获取formbizid
            String formBizId = ShardingActionHelper.constructBizId(billid, formId);

            // 根据当前信息使用id修改新建数据
            StringBuilder updateSql = new StringBuilder("update ");
            updateSql.append(maintablename);
            updateSql.append(" set qkpch='");
            updateSql.append(qkpch);
            updateSql.append("', zjly='");
            updateSql.append(zjly);
            updateSql.append("', ACT_TP='");
            updateSql.append(actTp);
            updateSql.append("', fl='");
            updateSql.append(fl);
            updateSql.append("', fkzy='");
            updateSql.append(fkzy);
            updateSql.append("', hzje='");
            updateSql.append(hzje);
            updateSql.append("', hblx='");
            updateSql.append(hblx);
            updateSql.append("', jsfs='");
            updateSql.append(jsfs);
            updateSql.append("', ");
            updateSql.append(" fkzh='");
            updateSql.append(fkzh);
            updateSql.append("', ");
            updateSql.append(" fkzhkhh='");
            updateSql.append(fkzhkhh);
            updateSql.append("', ");
            updateSql.append(" fkzhzh='");
            updateSql.append(fkzhzh);
            updateSql.append("', ");
            updateSql.append(" fkzhyhzh='");
            updateSql.append(fkzhyhzh);
            updateSql.append("', ");
            updateSql.append(" skzh='");
            updateSql.append(skzh);
            updateSql.append("', ");
            updateSql.append(" skzhkhh='");
            updateSql.append(skzhkhh);
            updateSql.append("', ");
            updateSql.append(" skzhzh='");
            updateSql.append(skzhzh);
            updateSql.append("', ");
            updateSql.append(" skzhyhzh='");
            updateSql.append(skzhyhzh);
            updateSql.append("', ");
            if (sqmwh != null  && !"".equals(sqmwh)) {
                updateSql.append(" sqmwh='");
                updateSql.append(sqmwh);
                updateSql.append("', ");
            }

            updateSql.append(" form_biz_id = '");
            updateSql.append(formBizId);
            updateSql.append("' where id = ");
            updateSql.append(billid);
            log.info("updateSql：" + updateSql.toString());
            rst.executeUpdate(updateSql.toString());
            // 提交事务
            rst.commit();

            // 权限重构
            ModeRightInfo modeRightInfo = new ModeRightInfo();
            modeRightInfo.setNewRight(true);
            modeRightInfo.editModeDataShare(user.getUID(), formmodeId, billid);

        } catch (Exception e) {
            // 回滚
            rst.rollback();
            ClearFundButtonRelated.deleteByBillId(maintablename, billid);
            log.error("添加数据异常：" + e.getMessage());
        }
    }

    /**
     * 查询映射表对应银行账号信息
     *
     * @param maintablename
     * @param zjly
     * @param actTp
     * @param sqmwh
     */
    public static JSONObject selectAccInfo(String maintablename, String zjly, String actTp, String sqmwh) {

        RecordSet rs = new RecordSet();

        JSONObject json = null;
        try {
            // 根据当前信息使用id修改新建数据
            StringBuilder sql = new StringBuilder("select fkzh,fkzhkhh,fkzhzh,fkzhyhzh,skzh,skzhkhh,skzhzh,skzhyhzh,fkdw,skdw,szd,skzhszd from ");
            sql.append(maintablename);
            sql.append(" where zjly = '");
            sql.append(zjly);
            sql.append("' ");
            sql.append("and ");
            sql.append(" ACT_TP = '");
            sql.append(actTp);
            sql.append("' ");
            if(sqmwh != null && !"".equals(sqmwh)){
                sql.append("and ");
                sql.append(" sqmwh = '");
                sql.append(sqmwh);
                sql.append("' ");
            }
            log.info("sql：" + sql.toString());
            rs.executeQuery(sql.toString());
            while (rs.next()) {
                json = new JSONObject();
                json.put("fkzh", Util.null2String(rs.getString("fkzh")));
                json.put("fkzhkhh", Util.null2String(rs.getString("fkzhkhh")));
                json.put("fkzhzh", Util.null2String(rs.getString("fkzhzh")));
                json.put("fkzhyhzh", Util.null2String(rs.getString("fkzhyhzh")));
                json.put("skzh", Util.null2String(rs.getString("skzh")));
                json.put("skzhkhh", Util.null2String(rs.getString("skzhkhh")));
                json.put("skzhzh", Util.null2String(rs.getString("skzhzh")));
                json.put("skzhyhzh", Util.null2String(rs.getString("skzhyhzh")));
                json.put("fkdw", Util.null2String(rs.getString("fkdw")));
                json.put("skdw", Util.null2String(rs.getString("skdw")));
                json.put("szd", Util.null2String(rs.getString("szd")));
                json.put("skzhszd", Util.null2String(rs.getString("skzhszd")));
            }
        } catch (Exception e) {
            log.error("查询数据异常：" + e.getMessage());
        }

        return json;
    }
}
